﻿using System.Configuration;

namespace EPiServer.Labs.MobilePack.Core
{
    public class MobilePackSection : ConfigurationSection
    {
        public static MobilePackSection CurrentSection
        {
            get
            {
                var sec = System.Web.Configuration.WebConfigurationManager.GetSection("mobilepack") as MobilePackSection;

                if (sec == null)
                {
                    return GetDefaults();
                }

                return sec;
            }
        }

        [ConfigurationProperty("previewpagetypes", IsRequired = false, DefaultValue = "[MobilePack]")]
        public string PreviewPageTypes
        {
            get
            {
                return (string)base["previewpagetypes"];
            }
            set
            {
                base["previewpagetypes"] = value;
            }
        }

        [ConfigurationProperty("virtualImageCacheName", IsRequired = false, DefaultValue = "mobilePack")]
        public string VirtualImageCacheName
        {
            get
            {
                return (string)this["virtualImageCacheName"];
            }
            set
            {
                this["virtualImageCacheName"] = value;
            }
        }

        [ConfigurationProperty("lifetimeImageCache", IsRequired = false, DefaultValue = "7")]
        public int LifetimeImageCache
        {
            get
            {
                return (int)this["lifetimeImageCache"];
            }
            set
            {
                this["lifetimeImageCache"] = value;
            }
        }

        [ConfigurationProperty("devices", IsDefaultCollection = true, IsRequired = false)]
        public DeviceCollection Devices
        {
            get { return (DeviceCollection)base["devices"]; }
            set { base["devices"] = value; }
        }

        [ConfigurationProperty("preview", IsDefaultCollection = false, IsRequired = false)]
        public DeviceCollection Preview
        {
            get { return (DeviceCollection)base["preview"]; }
            set { base["preview"] = value; }
        }

        [ConfigurationProperty("disablepreview", IsRequired = false, DefaultValue = false)]
        public bool DisablePreview
        {
            get
            {
                return (bool)base["disablepreview"];
            }
        }

        [ConfigurationProperty("disableServerImageResizing", IsRequired = false, DefaultValue = false)]
        public bool DisableServerImageResizing
        {
            get
            {
                return (bool)base["disableServerImageResizing"];
            }
        }

        public static MobilePackSection GetDefaults()
        {
            var rt = new MobilePackSection();
            rt.Devices = new DeviceCollection();
            rt.Preview = new DeviceCollection();

            rt.Devices.Add(new DeviceElement() { Name = "Android Device", UserAgent = "Android" });
            rt.Devices.Add(new DeviceElement() { Name = "Apple Device", UserAgent = "Apple" });
            rt.Devices.Add(new DeviceElement() { Name = "Apple iPhone", UserAgent = "iPhone" });
            rt.Devices.Add(new DeviceElement() { Name = "Apple iPod", UserAgent = "iPod" });
            rt.Devices.Add(new DeviceElement() { Name = "Apple iPad", UserAgent = "iPad" });
            rt.Devices.Add(new DeviceElement() { Name = "BlackBerry", UserAgent = "BlackBerry" });
            rt.Devices.Add(new DeviceElement() { Name = "Internet Explorer browser", UserAgent = "MSIE" });
            rt.Devices.Add(new DeviceElement() { Name = "Opera browser", UserAgent = "Opera" });
            rt.Devices.Add(new DeviceElement() { Name = "Firefox browser", UserAgent = "Firefox" });
            rt.Devices.Add(new DeviceElement() { Name = "Safari browser", UserAgent = "Safari" });
            rt.Devices.Add(new DeviceElement() { Name = "Chrome browser", UserAgent = "Chrome" });
            rt.Devices.Add(new DeviceElement() { Name = "Webkit browsers", UserAgent = "Webkit" });
            rt.Preview.Add(new DeviceElement() { Name = "Low Res (640x480)", Style = "mobilepack-phone-h" });
            rt.Preview.Add(new DeviceElement() { Name = "iPhone Vertical", Style = "mobilepack-iphone-v", Image = "images/iphonev.png" });
            rt.Preview.Add(new DeviceElement() { Name = "iPhone Horizontal", Style = "mobilepack-iphone-h", Image = "images/iphoneh.png" });
            rt.Preview.Add(new DeviceElement() { Name = "iPad Vertical", Style = "mobilepack-ipad-v", Image = "images/ipadv.png" });
            rt.Preview.Add(new DeviceElement() { Name = "iPad Horizontal", Style = "mobilepack-ipad-h", Image = "images/ipadh.png" });
            rt.Preview.Add(new DeviceElement() { Name = "Android Vertical", Style = "mobilepack-htc-v", Image = "images/htcv.png" });
            rt.Preview.Add(new DeviceElement() { Name = "Android Horizontal", Style = "mobilepack-htc-h", Image = "images/htch.png" });
            rt.Preview.Add(new DeviceElement() { Name = "Blackberry", Style = "mobilepack-blackberry-h", Image = "images/blackberryh.png" });

            rt.PreviewPageTypes = "[MobilePack]";

            return rt;    
        }
    }
}
